package com.ruoyi.web.controller.system;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCourseinfo;
import com.ruoyi.system.service.impl.SysCourseInfoServiceImpl;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * VIEWController
 *
 * @author ruoyi
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/system/cou_info")
public class SysCouresController extends BaseController {
    @Autowired
    private SysCourseInfoServiceImpl sysCourseInfoServiceImpl;

    /**
     * 查询VIEW列表
     */
    @PreAuthorize("@ss.hasAnyRoles('teacher,manger')")
    @GetMapping("/list")
    public TableDataInfo list(SysCourseinfo topicStuInfo) {
        startPage();
        List<SysCourseinfo> list = sysCourseInfoServiceImpl.selectTopicStuInfoList(topicStuInfo);
        return getDataTable(list);
    }

    /**
     * 导出VIEW列表
     */
    @PreAuthorize("@ss.hasAnyRoles('teacher,manger')")
    @Log(title = "VIEW", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCourseinfo topicStuInfo) {
        List<SysCourseinfo> list = sysCourseInfoServiceImpl.selectTopicStuInfoList(topicStuInfo);
        ExcelUtil<SysCourseinfo> util = new ExcelUtil<SysCourseinfo>(SysCourseinfo. class);
        util.exportExcel(response, list, "VIEW数据");
    }

    /**
     * 获取VIEW详细信息
     */
    @PreAuthorize("@ss.hasAnyRoles('teacher,manger')")
    @GetMapping(value = "/{name}")
    public AjaxResult getInfo(@PathVariable("name") String name) {
        return AjaxResult.success(sysCourseInfoServiceImpl.selectTopicStuInfoByName(name));
    }

    /**
     * 新增VIEW
     */
    @PreAuthorize("@ss.hasAnyRoles('teacher,manger')")
    @Log(title = "VIEW", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCourseinfo topicStuInfo) {
        return toAjax(sysCourseInfoServiceImpl.insertTopicStuInfo(topicStuInfo));
    }

    /**
     * 修改VIEW
     */
    @PreAuthorize("@ss.hasAnyRoles('teacher,manger')")
    @Log(title = "VIEW", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCourseinfo topicStuInfo) {
        return toAjax(sysCourseInfoServiceImpl.updateTopicStuInfo(topicStuInfo));
    }

    /**
     * 删除VIEW
     */
    @PreAuthorize("@ss.hasAnyRoles('teacher,manger')")
    @Log(title = "VIEW", businessType = BusinessType.DELETE)
    @DeleteMapping("/{names}")
    public AjaxResult remove(@PathVariable String[] names) {
        return toAjax(sysCourseInfoServiceImpl.deleteTopicStuInfoByNames(names));
    }

    @PreAuthorize("@ss.hasAnyRoles('teacher,manger')")
    @Log(title = "imp", businessType = BusinessType.DELETE)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysCourseinfo> util = new ExcelUtil<SysCourseinfo>(SysCourseinfo.class);
        List<SysCourseinfo> courseinfos = util.importExcel(file.getInputStream());
        for(SysCourseinfo courseinfo:courseinfos){

            if(sysCourseInfoServiceImpl.findAn(courseinfo.getStudentid())>0&&sysCourseInfoServiceImpl.findAn(courseinfo.getTeacherid())>0){
                sysCourseInfoServiceImpl.importData(courseinfo);
            }
        }
        return AjaxResult.success("导入成功");
    }
    @PreAuthorize("@ss.hasAnyRoles('teacher,manger')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysCourseinfo> util = new ExcelUtil<SysCourseinfo>(SysCourseinfo.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
